package com.whnk.attendance.service.impl;

import com.github.pagehelper.PageInfo;
import com.whnk.attendance.domain.PmProLevel;
import com.whnk.attendance.mapper.PmProInfoMapper;
import com.whnk.attendance.mapper.PmProLevelMapper;
import com.whnk.attendance.service.IPmProLevelService;
import com.whnk.attendance.vo.PmProListVo;
import com.whnk.attendance.vo.PmProQueryVo;
import com.whnk.attendance.vo.PmProUpdateVo;
import com.whnk.common.core.constant.HttpStatus;
import com.whnk.common.core.exception.ServiceException;
import com.whnk.common.core.web.page.TableDataInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目级别列Service业务层处理
 *
 * @author whnk
 * @date 2021-11-17
 */
@Service
public class PmProLevelServiceImpl implements IPmProLevelService
{
    @Autowired
    private PmProLevelMapper pmProLevelMapper;

    @Autowired
    private PmProInfoMapper pmProInfoMapper;

    /**
     * 查询项目级别列
     *
     * @param proId 项目级别列主键
     * @return 项目级别列
     */
    @Override
    public PmProListVo selectPmProLevelByProId(Long proId)
    {
        PmProListVo pmProListVo = new PmProListVo();
        PmProLevel pmProLevel = pmProLevelMapper.selectPmProLevelByProId(proId);
        BeanUtils.copyProperties(pmProLevel,pmProListVo);
        return pmProListVo;
    }

    /**
     * 查询项目级别列列表
     *
     * @param pmProQueryVo 项目级别列
     * @return 项目级别列
     */
    @Override
    public List<PmProListVo> selectPmProLevelList(PmProQueryVo pmProQueryVo)
    {
        PmProLevel pmProLevel = new PmProLevel();
        BeanUtils.copyProperties(pmProQueryVo,pmProLevel);
        List<PmProLevel> pmProLevels = pmProLevelMapper.selectPmProLevelList(pmProLevel);
        List<PmProListVo> pmProListVos = pmProLevels.stream().map(item -> {
            PmProListVo pmProListVo = new PmProListVo();
            BeanUtils.copyProperties(item,pmProListVo);
            return pmProListVo;
        }).collect(Collectors.toList());
        return pmProListVos;
    }

    @Override
    public TableDataInfo selectPmProLevelListTable(PmProQueryVo pmProQueryVo) {
        PmProLevel pmProLevel = new PmProLevel();
        BeanUtils.copyProperties(pmProQueryVo,pmProLevel);
        List<PmProLevel> pmProLevels = pmProLevelMapper.selectPmProLevelList(pmProLevel);
        List<PmProListVo> pmProListVos = pmProLevels.stream().map(item -> {
            PmProListVo pmProListVo = new PmProListVo();
            BeanUtils.copyProperties(item,pmProListVo);
            return pmProListVo;
        }).collect(Collectors.toList());
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(pmProListVos);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(pmProLevels).getTotal());
        return rspData;
    }

    /**
     * 新增项目级别列
     *
     * @param pmProUpdateVo 项目级别列
     * @return 结果
     */
    @Override
    public int insertPmProLevel(PmProUpdateVo pmProUpdateVo)
    {
        PmProLevel pmProLevel = new PmProLevel();
        BeanUtils.copyProperties(pmProUpdateVo,pmProLevel);
        pmProLevel.setProName(pmProInfoMapper.selectProName(pmProUpdateVo.getProNum()));
        return pmProLevelMapper.insertPmProLevel(pmProLevel);
    }

    /**
     * 修改项目级别列
     *
     * @param pmProUpdateVo 项目级别列
     * @return 结果
     */
    @Override
    public int updatePmProLevel(PmProUpdateVo pmProUpdateVo)
    {
        PmProLevel pmProLevel = new PmProLevel();
        BeanUtils.copyProperties(pmProUpdateVo,pmProLevel);
        pmProLevel.setProName(pmProInfoMapper.selectProName(pmProUpdateVo.getProNum()));
        return pmProLevelMapper.updatePmProLevel(pmProLevel);
    }

    /**
     * 批量删除项目级别列
     *
     * @param proIds 需要删除的项目级别列主键
     * @return 结果
     */
    @Override
    public int deletePmProLevelByProIds(Long[] proIds)
    {
        return pmProLevelMapper.deletePmProLevelByProIds(proIds);
    }

    /**
     * 删除项目级别列信息
     *
     * @param proId 项目级别列主键
     * @return 结果
     */
    @Override
    public int deletePmProLevelByProId(Long proId)
    {
        return pmProLevelMapper.deletePmProLevelByProId(proId);
    }


    /**
     * 导入
     * @param pmProListVos
     */
    @Override
    public String pmProLeverUpload(List<PmProListVo> pmProListVos,boolean updateSupport) {
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (PmProListVo pmProListVo : pmProListVos) {
            try {
                PmProLevel pmProLevel = new PmProLevel();
                pmProLevel.setProNum(pmProListVo.getProNum());
                pmProLevel.setBankLevelNum(pmProListVo.getBankLevelNum());
                List<PmProLevel> pmProLevels = pmProLevelMapper.selectPmProLevelList(pmProLevel);
                if (CollectionUtils.isEmpty(pmProLevels)){
                    BeanUtils.copyProperties(pmProListVo, pmProLevel);
                    pmProLevelMapper.insertPmProLevel(pmProLevel);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、项目级别名称为:" + pmProListVo.getProName()+"/t" +"行方评级名称为:"+pmProListVo.getBankLevelName()+"/t" +"的数据导入成功");
                }
                else if (updateSupport)
                {
                    pmProListVo.setProId(pmProLevels.get(0).getProId());
                    BeanUtils.copyProperties(pmProListVo, pmProLevel);
                    pmProLevelMapper.updatePmProLevel(pmProLevel);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、项目级别名称为:" + pmProListVo.getProName()+"/t" +"行方评级名称为:"+pmProListVo.getBankLevelName()+"/t" + "的数据更新成功");
                }else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、项目级别名称为:" + pmProListVo.getProName()+"/t" +"行方评级名称为:"+pmProListVo.getBankLevelName()+"/t"+ " 的数据已存在");
                }
            } catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、项目级别名称为:" + pmProListVo.getProName()+"/t" +"行方评级名称为:"+pmProListVo.getBankLevelName()+"/t" + " 的数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
